import type { InjectionKey } from 'vue'
import { createStore, useStore as baseUseStore, Store } from 'vuex'
import { setToken } from '@/utils/auth'
import { getAreaList, getAccountInfo } from '@/api/system'
import { DealData } from '@/utils/utils'
import { Area } from '@/api/types/area'

export interface Config {
  mapConfig: any
  apiHost: string,
  apiHostMain: string,
  areaCode: string | number
  appName: string,
  fileUrl: string,
  videoApiHost: string
  videoDownload: string
  videoToken: string
  sources: string
  arcgisUrl: string
  darwinUrl: string
}

export interface User {
  name: string | null
  permissions: string[]
  roles: string[]
  sysArea: string
  sysOrgan: string
}

const state = {
  config: {} as Config,
  user: {} as User,
  dictionary: {} as {[key: string]: any},
  dictionaryMap: {} as {[key: string]: any},
  areaList: [] as Area[]
}

export type State = typeof state

// 定义 injection key
export const key: InjectionKey<Store<State>> = Symbol('store')

export const store = createStore<State>({
  state,
  mutations: {
    // 配置信息
    setConfig (state, data: Config) {
      state.config = data
      setToken('unify-config', data)
    },
    // 设置dict
    setDictionary (state, dict) {
      state.dictionary = dict
    },
    // 设置dictMap
    setDictionaryMap (state, dict) {
      state.dictionaryMap = dict
    },
    // 设置行政区划
    setAreaList (state, areaList) {
      state.areaList = areaList
    },
    // 设置用户信息
    setUserInfo (state, userInfo:User) {
      state.user = userInfo
    }
  },
  actions: {
    // 设置全局字典数据
    async setDictionarySync ({ commit }) {
      try {
        // const { content: dicrContent } = await getDictList({ page: 0, size: 10000 }, true)
        const dicrContent:any = []
        const dictList = DealData(dicrContent, item => {
          item.label = item.name
          item.value = item.code
        })
        const dict: { [key: string]: any } = {}
        const dictMap: { [key: string]: any } = {}
        dictList.forEach(item => {
          dict[item.code] = item.children
          const map: { [key: string]: any } = {}
          item.children && item.children.forEach((child: any) => {
            map[child.code] = child.name
          })
          dictMap[item.code] = map
        })
        commit('setDictionary', dict)
        commit('setDictionaryMap', dictMap)
      } catch (e) {}
      try {
        const accountInfo = await getAccountInfo()
        const code = accountInfo.sysAreaCode.replace(/(0+)$/g, '')
        const { content: areaList } = await getAreaList({ code: code, page: 0, size: 10000 })
        commit('setAreaList', areaList)
        commit('setUserInfo', accountInfo)
      } catch (e) {}
      // try {
      //   // const { content: orgList } = await getOrganList({ page: 0, size: 10000 })
      //   // const orgList:any = []
      //   // commit('setOrgList', orgList)
      // } catch (e) {}
    }
  },
  getters: {
    // 获取级联area数据
    getAreaList: state => DealData(state.areaList, item => {
      item.label = item.name
      item.value = item.code
    }),
    // 获取用户信息
    getUserInfo: state => {
      return state.user
    }
  }
})

export function useStore () {
  return baseUseStore(key)
}
